<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * -----------------------------------------------------
 *
 * controllers home
 *
 * @package     controllers
 * @subpackage  
 * @category    signup
 * @author      Pupuchen
 * @since       2015-7-9 version 1.0       
 * -----------------------------------------------------    
 */
class signup extends CI_Controller {
	public function __construct()
	{
	  	parent::__construct();
	  	$this->load->model(array('member_model'));
	  	$this->load->library('my_md5'); 
	}

	/**
	 * [register 会员注册]
	 * @return [type] [description]
	 */
	public function register(){
		//如果已经登录,跳转到用户中心首页
        if (!empty($_SESSION['member_id']) && !empty($_SESSION['member_id']) > 0) {
            header('Location:'.base_url('member'));
            exit;
        }
		$this->load->view('home/member/register');
	}
	/**
	 * [login 会员登录]
	 * @return [type] [description]
	 */
	public function login(){
		//如果已经登录,跳转到用户中心首页
        if (!empty($_SESSION['member_id']) && !empty($_SESSION['member_id']) > 0) {
            header('Location:'.base_url('member'));
            exit;
        }
		$this->load->view('home/member/login');
	}
	/**
	 * [login 退出登录]
	 * @return [type] [description]
	 */
	public function logout(){
		unset($_SESSION['member_id']);
		unset($_SESSION['member_mobile']);
		header('Location:'.base_url('/'));
	}

	/**
	 * [reg 会员注册过程]
	 * @return [type] [description]
	 */
	public function reg(){
		$result = array(
            'err'=>'0',
            'msg'=>''
        );

		// 检查参数的合理性
		if(empty($_POST['mobile']) && empty($_POST['password']) && empty($_POST['captcha'])){
			$result['err'] = 1;
            $result['msg'] = '参数不正确,请重新输入';
            exit(json_encode($result));
		}
		//检查验证码是否属于正确
        if(strtoupper($_SESSION['captcha']) != strtoupper($_POST['captcha'])){
            $result['err'] = 2;
            $result['msg'] = '您的验证码有误请重新输入';
            exit(json_encode($result));
        }
        //检测手机号码是否存在
       	if($this->member_model->get_data_by_mobile($this->input->post('mobile',TRUE)) > 0){
            $result['err'] = 3;
            $result['msg'] = '此手机已经注册';
            exit(json_encode($result));
        }
        $_insert['mobile']     = $this->input->post('mobile',TRUE);
        $_insert['password']   = My_md5::to_md5($this->input->post('password',TRUE));
        $_insert['reg_time']   = time();
        $member_id = $this->member_model->insert($_insert); // 注册
        if(!is_int($member_id)){
        	$result['err'] = 4;
            $result['msg'] = '注册失败';
        }
        $_SESSION['member_id'] = $member_id; // 尝试登录
		$result['err'] = 0;
		exit(json_encode($result));
	}
	/**
	 * [log 登录过程]
	 * @return [type] [description]
	 */
	public function log(){
		$result = array(
            'err'=>'0',
            'msg'=>''
        );

		// 检查参数的合理性
		if(empty($_POST['mobile']) && empty($_POST['password'])){
			$result['err'] = 1;
            $result['msg'] = '参数不正确,请重新输入';
            exit(json_encode($result));
		}
        //检测手机号码是否存在
       	if($this->member_model->get_data_by_mobile($this->input->post('mobile',TRUE)) < 1){
            $result['err'] = 3;
            $result['msg'] = '手机号码不存在!';
            exit(json_encode($result));
        }

        $arr_member = $this->member_model->get_a_member_by_mobile($_POST['mobile']); // 会员信息 

       	if(count($arr_member) == 0){
			$result['err'] = 4;
            $result['msg'] = '此账户不存在!';
            exit(json_encode($result));
		}

		// 比对密码
		if($this->my_md5->md5_2($_POST['password']) != $this->my_md5->get_md5($arr_member['password'])){
			$result['err'] = 5;
            $result['msg'] = '密码不正确,请重新输入!';
            exit(json_encode($result));
		}

		// 记录id和会员昵称
		$_SESSION['member_id']     = $arr_member['id'];
		$_SESSION['member_mobile'] = $arr_member['mobile'];

		$_updata['login_time'] 	   = time(); // 登录时间
		$this->member_model->update($_updata,$arr_member['id']);// 更新会员登录的时间 
		// 登录成功
		$result['err'] = 0;
		exit(json_encode($result));
	}
	/**
	 * [captcha 发送注册短信]
	 * @return [type] [description]
	 */
	public function captcha(){
		$result = array(
	        'err'=>'0',
	        'msg'=>''
        );
		// 判断数据合理性
        if(empty($_GET['mobile'])){
        	$result['err'] = 1;
            $result['msg'] = '手机号码错误!';
        	return json_encode($result);
        }
        // 判断手机号码是否存在
        if($this->member_model->get_data_by_mobile($_GET['mobile']) > 0){
            $result['err'] = 2;
            $result['msg'] = '此手机已经注册';
            exit(json_encode($result));
        }
		$mobile 	= $_GET['mobile']; // 手机号码
		$this->load->library('mobile_sns',array('mobile'=>$mobile));   	
		$captcha = $this->mobile_sns->create_char();	
		// 把验证码加入session中			   
		$_SESSION['captcha'] = $captcha;
		$reg = $this->mobile_sns->yzm('您的注册验证码是'.$captcha.',请勿告诉他人,企多帮祝您生活愉快！');
		if($reg == 'ok'){
			$result['err'] = 0;
            exit(json_encode($result));
		}else{
			$result['err'] = 3;
            $result['msg'] = $this->mobile_sns->error;
            exit(json_encode($result));
		}
	}

	/**
	 * [setup 个人中心设置]
	 * @return [type] [description]
	 */
	public function setup(){
		echo '个人中心设置';
	}
	
}
